<?php

class Damages_DynamicClass {

  private $data;

  public function __construct($data) {
    if (is_array($data))
      $this->data = $data;
    else
      $this->data = array();
  }

  public function __get($name) {
    if (array_key_exists($name, $this->data)) {
      if (is_array($this->data[$name])) {
        return new Damages_DynamicClass($this->data[$name]);
      }
      return $this->data[$name];
    }
    return null;
  }

  public function __set($name, $value) {
    $this->data[$name] = $value;
  }

  public function getDefault($name) {
    if (array_key_exists($name, $this->data)) {
      $result = new stdClass();
      $result->item = $this->data[$name];
      return $result;
    }
    return null;
  }

  public function toArray() {
    $result = array();
    foreach ($this->data as $val) {
      $result[] = (is_array($val)) ? new Damages_DynamicClass($val) : $val;
    }
    return $result;
  }

}